#!/usr/bin/python
'''
Script to iterate through all nodes and install Java on each via scp and ssh.
'''

import nodes
import ssh

INSTALL_JAVA='tar -xvzf jre.tar.gz;echo PATH=/home/usf_ubcslice5/jre1.6.0_16/bin:$PATH >> .bash_profile;echo export PATH >> .bash_profile'
command = 'tar -xvzf monitord-1.0.tgz; sudo mv monitord/monitord.neighbours /var/cache/monitord.neighbours; sudo chmod 666 /var/cache/monitord.neighbours; sudo mv monitord /usr/local; sudo /usr/local/monitord/monitord.py &'
password = 'eece411'
src_file = '/home/kevin/monitord-1.0.tgz'

def deploy(password, command, src_file, 
           dst_file='/home/usf_ubcslice5/',
           username='usf_ubcslice5',
           keyfile='/home/kevin/p01/id_rsa' 
           ):
	#if __name__=='__main__':
	print 'Getting lists of hostnames...'
	hosts = hosts = ['napa4.mini.pw.edu.pl', 'napa2.mini.pw.edu.pl', 'napa1.mini.pw.edu.pl', 'napa3.mini.pw.edu.pl', 'napa1.tele.pw.edu.pl', 'napa2.tele.pw.edu.pl', 'nis-planet2.doshisha.ac.jp', 'nis-planet1.doshisha.ac.jp', 'node-1.mcgillplanetlab.org', 'netapp6.cs.kookmin.ac.kr', 'mtuplanetlab1.cs.mtu.edu', 'node0.planetlab.etl.luc.edu', 'mtuplanetlab2.cs.mtu.edu', 'node1.lbnl.nodes.planet-lab.org', 'netapp7.cs.kookmin.ac.kr', 'mnc2.pusan.ac.kr', 'mnc1.pusan.ac.kr', 'node-2.mcgillplanetlab.org', 'node1.planetlab.uprr.pr', 'node2.planetlab.uprr.pr', 'node2pl.planet-lab.telecom-lille1.eu', 'node1pl.planet-lab.telecom-lille1.eu', 'node1pl.planet-lab.telecom-lille1.eu', 'node2.planetlab.albany.edu', 'node3.planet-lab.titech.ac.jp', 'node1.planetlab.albany.edu', 'node2.lbnl.nodes.planet-lab.org', 'of-planet1.stanford.edu', 'node2.planet-lab.titech.ac.jp', 'node1.planetlab.etl.luc.edu', 'node4.planet-lab.titech.ac.jp', 'node1.planet-lab.titech.ac.jp', 'node1.planetlab.mathcs.emory.edu', 'nodeb.howard.edu', 'node2.planetlab.mathcs.emory.edu', 'nodea.howard.edu', 'cs-planetlab3.cs.surrey.sfu.ca', 'csplanet1.ncat.edu', 'agni.iitd.ernet.in', 'csplanet2.ncat.edu', 'splanet02.cs-ncl.net', 'csplanet01.cs-ncl.net', '147-179.surfsnel.dsl.internl.net', '146-179.surfsnel.dsl.internl.net', '75-130-96-13.static.oxfr.ma.charter.com', 'arari.snu.ac.kr', 'adrastea.mcs.suffolk.edu', 'charon.cs.binghamton.edu', 'ait05.us.es', 'aladdin.planetlab.extranet.uni-passau.de', '75-130-96-12.static.oxfr.ma.charter.com', 'cs-planetlab4.cs.surrey.sfu.ca', 'csplanetlab2.kaist.ac.kr', 'alice.cs.princeton.edu', 'chronos.disy.inf.uni-konstanz.de', 'adam.ee.ntu.edu.tw', 'datacomngn.cnu.ac.kr', 'eggplant.cs.princeton.edu', 'dplanet2.uoc.edu', 'dplanet1.uoc.edu', 'ds-pl3.technion.ac.il', 'ds-pl2.technion.ac.il', 'ds-pl1.technion.ac.il', 'csplanetlab1.kaist.ac.kr', 'csplanetlab3.kaist.ac.kr', 'csplanetlab4.kaist.ac.kr', 'deimos.cecalc.ula.ve', 'delta.cs.princeton.edu', 'ent2.cs.nccu.edu.tw', 'dannan.disy.inf.uni-konstanz.de', 'ebb.colgate.edu', 'dschinni.planetlab.extranet.uni-passau.de', 'earth.cs.brown.edu', 'dplab03.kookmin.ac.kr', 'ent1.cs.nccu.edu.tw', 'echo.cs.princeton.edu', 'gschembra3.diit.unict.it', 'gschembra4.diit.unict.it', 'int-pl2.ise.eng.osaka-u.ac.jp', 'int-pl1.ise.eng.osaka-u.ac.jp', 'fakenode.cs.princeton.edu', 'host3-plb.loria.fr', 'hptest-3.cs.princeton.edu', 'Iason.inf.uth.gr', 'host147-82-static.93-94-b.business.telecomitalia.it', 'host149-82-static.93-94-b.business.telecomitalia.it', 'host4-plb.loria.fr', 'host2.planetlab.informatik.tu-darmstadt.de', 'hptest-1.cs.princeton.edu', 'iraplab1.iralab.uni-karlsruhe.de', 'host3.planetlab.informatik.tu-darmstadt.de', 'fobos.cecalc.ula.ve', 'host1.planetlab.informatik.tu-darmstadt.de', 'flow.colgate.edu', 'eve.ee.ntu.edu.tw', 'freedom.informatik.rwth-aachen.de', 'itchy.tek.bth.se', 'marie.iet.unipi.it', 'kostis.di.uoa.gr', 'mlab3.gr-ix.gr', 'mlab1.gr-ix.gr', 'mlab2.gr-ix.gr', 'itchy.cs.uga.edu', 'medea.inf.uth.gr', 'lsirextpc01.epfl.ch', 'mercury.silicon-valley.ru', 'jupiter.cs.brown.edu', 'lsirextpc02.epfl.ch', 'kc-sce-plab1.umkc.edu', 'kc-sce-plab2.umkc.edu', 'lefthand.eecs.harvard.edu', 'metis.mcs.suffolk.edu', 'merkur.planetlab.haw-hamburg.de', 'iraplab2.iralab.uni-karlsruhe.de', 'mars.planetlab.haw-hamburg.de', 'kupl1.ittc.ku.edu', 'napa4.tele.pw.edu.pl', 'napa3.tele.pw.edu.pl', 'kupl2.ittc.ku.edu', 'planetlab4.cs.uiuc.edu']
	print len(hosts),' nodes found.'
	for number,hostname in enumerate(hosts):
		print '\tDeploying to node:',hostname,'(#'+str(number)+')'
		print 'Uploading %s ...' % (src_file)
		ok = ssh.upload(src_file, dst_file, password, username, keyfile, hostname)
		if ok: print '[PASS]' 
		else: 
			print '[FAIL]'
			print 'Moving on to the next node!'
			continue
		print 'Executing Command...',
		ok = ssh.command(command, password, username, keyfile, hostname)
		if ok: 
			print '[PASS]'
			continue
		else: print '[FAIL]'

